Flask-login 简单 Demo, 最简单的实现登录方法
- 当使用MethodView的时候,login_required 使用方法
1
2
3
4class XxxxView(MethodView):
decorators = [login_required] # 相当于 @login_required 装饰圈
def get(self):
主文件1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59from flask_sqlalchemy import SQLAlchemy
from flask import Flask, redirect, url_for, request
from flask_login import LoginManager, current_user, login_user, login_required
app = Flask(__name__)
app.secret_key = 'Sqsdsffqrhgh.,/1#$%^&'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./db/blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.debug = True
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
password = '123' #只要用户输入的密码是 123 就可以登录
def user_loader(id): #这个id参数的值是在 login_user(user)中传入的 user 的 id 属性
from model.model import User
user = User.query.filter_by(id=id).first()
return user
# 添加登录视图,如果是GET方法,返回一个简单的表单
def login():
from model.model import User
if request.method == 'GET':
return '''
<form action="#" method="POST">
<span>请输入账号</span>
<input type="text" name="name" id="name" placeholder="name">
<span>请输入密码</span>
<input type="password" name="pw" id="pw" placeholder="password">
<input type="submit" name="submit">
</form>
'''
name = request.form.get('name')
if request.form.get('pw') == password:
user = User.query.filter_by(name=name).first()
if not user:
user = User(name=name)
db.session.add(user)
db.session.commit()
login_user(user)
return redirect(url_for('index'))
return 'Bad login'
#如果密码是 123 就会跳转到视图函数 index 上
def index():
user = current_user
return 'HelloWorld'
if __name__ == '__main__':
app.run()
model.py1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17from model import *
'''
创建类的时候继承UserMixin ,有一些用户相关属性
* is_authenticated :是否被验证
* is_active : 是否被激活
* is_anonymous : 是否是匿名用户
* get_id() : 获得用户的id,并转换为 Unicode 类型
'''
class User(db.Model,UserMixin):
__tablename__ = 'login_users'
id = db.Column(db.Integer,primary_key=True)
name = db.Column(db.String(50),unique=True)
login_count = db.Column(db.Integer,default=0)
last_login_ip = db.Column(db.String(128),default='unknown')
稍微全面一点的1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70from flask_sqlalchemy import SQLAlchemy
from flask import Flask, redirect, url_for, request, flash, render_template
from flask_login import LoginManager, login_user, login_required, logout_user
app = Flask(__name__)
app.secret_key = 'Sqsdsffqrhgh.,/1#$%^&'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///./db/blog.db'
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = True
app.debug = True
db = SQLAlchemy(app)
login_manager = LoginManager()
login_manager.init_app(app)
login_manager.login_view = "login" # 定义登录的 视图
login_manager.login_message = '请登录以访问此页面' # 定义需要登录访问页面的提示消息
def user_loader(id): # 这个id参数的值是在 login_user(user)中传入的 user 的 id 属性
from model.model import User
user = User.query.filter_by(id=id).first()
return user
# 添加登录视图,如果是GET方法,返回一个简单的表单
def login():
from model.model import User
if request.method == 'POST':
name = request.form.get('name')
user = User.query.filter_by(name=name).first()
if not user:
flash('该用户不存在')
elif request.form.get('pwd') != user.pwd:
flash('密码错误')
else:
login_user(user, remember=True)
next_url = request.args.get('next')
return redirect(next_url or url_for('login_success'))
return render_template('login.html') # 如果密码是 123 就会跳转到视图函数 index 上
def index():
return 'Hello Tank'
def login_success():
return render_template('base.html')
def logout():
logout_user() # 登出用户
return '已经退出登录'
def page_not_found(error):
return render_template('page_not_found.html'), 404
if __name__ == '__main__':
app.run()